'use strict';
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
const u = require('../../nodes/Scalar.cjs'),
  h = require('../../stringify/stringifyString.cjs'),
  d = {
    identify: e => e instanceof Uint8Array,
    default: !1,
    tag: 'tag:yaml.org,2002:binary',
    resolve(e, t) {
      if (typeof Buffer == 'function') return Buffer.from(e, 'base64');
      if (typeof atob == 'function') {
        const a = atob(e.replace(/[\n\r]/g, '')),
          r = new Uint8Array(a.length);
        for (let n = 0; n < a.length; ++n) r[n] = a.charCodeAt(n);
        return r;
      } else
        return (
          t(
            'This environment does not support reading binary tags; either Buffer or atob is required',
          ),
          e
        );
    },
    stringify({ comment: e, type: t, value: a }, r, n, c) {
      const s = a;
      let i;
      if (typeof Buffer == 'function')
        i =
          s instanceof Buffer
            ? s.toString('base64')
            : Buffer.from(s.buffer).toString('base64');
      else if (typeof btoa == 'function') {
        let o = '';
        for (let f = 0; f < s.length; ++f) o += String.fromCharCode(s[f]);
        i = btoa(o);
      } else
        throw new Error(
          'This environment does not support writing binary tags; either Buffer or btoa is required',
        );
      if ((t || (t = u.Scalar.BLOCK_LITERAL), t !== u.Scalar.QUOTE_DOUBLE)) {
        const o = Math.max(
            r.options.lineWidth - r.indent.length,
            r.options.minContentWidth,
          ),
          f = Math.ceil(i.length / o),
          g = new Array(f);
        for (let l = 0, b = 0; l < f; ++l, b += o) g[l] = i.substr(b, o);
        i = g.join(
          t === u.Scalar.BLOCK_LITERAL
            ? `
`
            : ' ',
        );
      }
      return h.stringifyString({ comment: e, type: t, value: i }, r, n, c);
    },
  };
exports.binary = d;
